/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao;

import java.sql.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import model.ConnectionFactory;
import model.beans.TipoConsulta;

/**
 *
 * @author marcos
 */
public class TipoConsultaDAO {
    private Connection con;
    private PreparedStatement stmtInserir;
    private PreparedStatement stmtListar;
    private PreparedStatement stmtBuscar;
    private PreparedStatement stmtAlterar;
    private PreparedStatement stmtDeletar;
    private PreparedStatement stmtConsultas;
    
    public TipoConsultaDAO(){
        try{
            con = ConnectionFactory.getConnection();
        }catch(Exception ex){
            ex.getMessage();
        }
    }
    
    public TipoConsultaDAO(Connection con){
        try{
            this.con = con;
        }catch(Exception ex){
            ex.getMessage();
        }
    }
    
    public void inserir(TipoConsulta tpcon){
        try{
            stmtInserir = con.prepareStatement("INSERT INTO tipoconsulta (nome, duracao) VALUES (?,?);");
            stmtInserir.setString(1, tpcon.getNome());
            stmtInserir.setString(2, tpcon.getDuracao());
            stmtInserir.executeUpdate();
        }catch(Exception ex){
            ex.getMessage();
        }
    }
    
    public void alterar(TipoConsulta tp){
        try{
            stmtAlterar = con.prepareStatement("UPDATE tipoconsulta set nome = ?, duracao = ? WHERE idTpCons = ?");
            stmtAlterar.setString(1, tp.getNome());
            stmtAlterar.setString(2, tp.getDuracao());
            stmtAlterar.setInt(3, tp.getIdTpCons());
            stmtAlterar.executeUpdate();
        }catch(Exception ex){
            ex.getMessage();
        }
    }
    
    public List<TipoConsulta> listar(){
        try{
            stmtListar = con.prepareStatement("SELECT * FROM tipoconsulta;");
            ResultSet rs = stmtListar.executeQuery();
            List<TipoConsulta> lista = new ArrayList();

            while(rs.next()){
                TipoConsulta tpcon = new TipoConsulta();
                tpcon.setIdTpCons(rs.getInt("idtpcons"));
                tpcon.setNome(rs.getString("nome"));
                tpcon.setDuracao(rs.getString("duracao"));

                lista.add(tpcon);
            }
            return lista;
        }catch(Exception ex){
            return null;
        }
    }
    
    public TipoConsulta buscar(int id){
        try{
            stmtBuscar = con.prepareStatement("SELECT * FROM tipoconsulta WHERE idTpCons= ?");
            stmtBuscar.setInt(1, id);
            ResultSet rs = stmtBuscar.executeQuery();
            TipoConsulta tpcon = new TipoConsulta();
            if(rs.next()){
                tpcon.setIdTpCons(rs.getInt("idtpcons"));
                tpcon.setNome(rs.getString("nome"));
                tpcon.setDuracao(rs.getString("duracao"));
            }
            return tpcon;
        }catch(Exception ex){
            return null;
        }
    }
    
    public void deletar(int id){
        try{
            stmtDeletar = con.prepareStatement("DELETE FROM tipoconsulta WHERE idTpCons = ?");
            stmtDeletar.setInt(1, id);
            stmtDeletar.executeUpdate();
        }catch(Exception ex){
            ex.getMessage();
        }
    }
    
    public boolean consultas(int id) throws SQLException{
        stmtConsultas = con.prepareStatement("SELECT * FROM tipoconsulta t, consulta co WHERE t.idTpCons = co.idTpCons AND t.idTpCons = ?");
        stmtConsultas.setInt(1, id);
        ResultSet rs = stmtConsultas.executeQuery();
        if(rs.next()){
            return false;
        }else{
            return true;
        }
    }
    
        @Override
    public void finalize() throws Throwable{
        super.finalize();
        stmtInserir.close();
        stmtListar.close();
        stmtAlterar.close();
        stmtBuscar.close();
        stmtDeletar.close();
        con.close();
    }
}
